<?php
session_start();
//require_once "../../PasswordHashClass.php";
//$DB = new DB('sqlite::memory:');

require_once('../includes/DbConnector.php');
$connector  = new DbConnector();

if (isset($_POST['username']) && isset($_POST['password'])) {
	
	// Other validation logic can take place here
	
	$hashed = create_hash($_POST['password']);
        
        $sql = "SELECT * FROM user_account WHERE username = '{$_POST['username']}'";
	$result = $connector->query($sql);
//	
        while($row = mysql_fetch_object($result)){
            $user = $row;
      }
	if (!empty($user)) {
            echo 'USERNAME TAKEN';
		//$_SESSION['msg'] = "Username already in use.";
		//header("Location: /signUp.php");
	} else {
            $sql2 = "INSERT INTO user_account(username, email, first_name, last_name, password, status_id, account_type, user_ranking_id) 
                        VALUES ('{$_POST['username']}','{$_POST['email']}','{$_POST['firstname']}','{$_POST['lastname']}','$hashed',1,0,1);";
            $insresult = $connector->query($sql2);
//            
            if($insresult) {
                $a = mysql_insert_id();
                echo json_encode($a);
//                echo 'alt gikk bra';
                //$_SESSION['msg'] = "Your registration was successful!";
                //header("Location: /barStats.php");
            } else {
                echo 'noe gikk galt';
                //$_SESSION['msg'] = "An error has occurred.";
                //header("Location: /Barcrawl/admin/signUp.php");
            }
	}
	exit;
} else {
	// Just a basic HTML form
}
?>